import axios from "axios"
import { getSession } from "@/util/tools"

import { baseUrl } from "./requestApi"

const service = axios.create({
  baseURL: baseUrl,
  headers: {
    "Content-Type": "application/json;charset=UTF-8"
  },
  timeout: 600000
})
// 请求拦截
service.interceptors.request.use(
  (config) => {
    const token = getSession("token") || ""
    if (!config.noToken) {
      config.headers["Authorization"] = token ? "Bearer " + token : ""
    }
    if (config.isFormData) {
      config.headers["Content-Type"] = "application/vnd.ms-excel"
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use((response) => {
  const res = response.data
  return res
})

export default service
